var index = parent.layer.getFrameIndex(window.name);
var task_number = parent.$("#taskNumber").val();
var currentBlockId = parent.$("#blockid").val();
var frontAlgorithmIds = parent.$("#frontAlgorithmIds").val();
var json_frontAlgorithmIds = eval("(" + frontAlgorithmIds + ")");
var output = parent.$("#output").val();
var allowModification = parent.$("#allowModification").val();
var allIsFile = true;
window.onload = function () {
    $.ajax({
        url: constUrl + "ws/model",
        type: 'post',
        dataType: 'json',
        data: {
            "operateType": "configModel",
            "data": JSON.stringify({
                "currentAlgorithmId": "",
                "currentBlockId": currentBlockId,
                "frontAlgorithmIds": json_frontAlgorithmIds
            })
        },
        success: function (responseText) {
            var jsonData = responseText;
            $("#task_number").val(task_number);
            creatHtml(jsonData);
            if (output != "" && output != null) {
                var json_output = eval("(" + output + ")");
                var i = 0;
                $("#outputTabContent").children(".tab-custom").each(function (index, elem) {
                    var $elem = $(elem);
                    var output_temp = json_output[i];
                    console.log(output_temp);
                    var j = 0;
                    $elem.children(".oneBlock").children(".input").each(function (pindex, pelem) {
                        var $pelem = $(pelem);
                        var temp = output_temp[j].outputAddress + output_temp[j].outputValue;
                        $pelem.children(".outputValue").val(output_temp[j].outputValue);
                        $pelem.children(".outputAddress").val(output_temp[j].outputAddress);
                        $pelem.children(".output").val(temp);
                        $pelem.children(".output").attr("title", temp);
                        j++;
                    });
                    i++;
                });
            }
            if(allowModification=="false"){
                $("#y-title").append("--查看模式");
                $(":button").attr("disabled", true);
                $("input[type=text]").attr("readonly", true);
            }
        },
        complete: function () {
        },
        error: function () {
            layer.alert("获取数据失败！")
        }
    })
    $('#saveSetting').click(function () {
        if(allowModification=="true") {
            var outputConfig = "";
            var one_b = [];
            var success = true;
            if ($("#custom").val() == "1") {
                $("#output_form").children(".table_div").children("#outputTabContent").children(".tab-custom").each(function (pindex, pelem) {
                    var $pelem = $(pelem);
                    var one_block = $pelem.children(".oneBlock");
                    one_b = [];
                    one_block.each(function (index, elem) {
                        var $elem = $(elem);
                        var cinput = $elem.children(".input");
                        cinput.each(function (cindex, celem) {
                            var $celem = $(celem);
                            if ($celem.children('.outputValue').val() == "") {
                                console.log("sss:" + $celem.children('.outputValue').val());
                                success = false;
                            }
                            one_b.push({
                                blockId: $elem.attr("blockid"),
                                blockName: $elem.attr("blockName"),
                                outputName: $celem.children('.input_name').children('label').children(".input_name_text").text(),
                                outputAddress: $celem.children('.outputAddress').val(),
                                outputValue: $celem.children('.outputValue').val()
                            });
                        });
                    });
                    var json_one_block = JSON.stringify(one_b);
                    outputConfig += "," + json_one_block;
                });
            } else {
                var one_block = $("#output_form").children(".table_div").children("#outputTabContent").children(".tab-del");
                one_b = [];
                one_block.children(".defaultBlock").each(function (index, elem) {
                    var $elem = $(elem);
                    var cinput = $elem.children(".input");
                    cinput.each(function (cindex, celem) {
                        var $celem = $(celem);
                        if ($celem.children('.outputValue').val() == "") {
                            success = false;
                        }
                        one_b.push({
                            blockId: $elem.attr("blockid"),
                            blockName: $elem.attr("blockName"),
                            outputName: $celem.children('.input_name').children('label').children(".input_name_text").text(),
                            outputAddress: $celem.children('.outputAddress').val(),
                            outputValue: $celem.children('.outputValue').val()
                        });
                    });
                });
                var json_one_block = JSON.stringify(one_b);
                for (var i = 0; i < $("#task_number").val(); i++) {
                    outputConfig += "," + json_one_block;
                }
            }
            outputConfig = outputConfig.substr(1);
            outputConfig = "[" + outputConfig + "]";
            if (success) {
                window.parent.document.getElementById("output").value = outputConfig;
                parent.layer.close(index);
            } else {
                layer.alert("请配置所有输出文件！");
            }
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
    $('#closeIframe').click(function () {
        parent.layer.close(index);
    });
    $('#defaultPath').click(function () {
        if (allIsFile){
            var defaultPath_temp = $("#defaultPathTemp").val();
            var defaultPath_temp_mod = defaultPath_temp.substr(1);
            if (defaultPath_temp!=""&&defaultPath_temp!=null){
                $(".defaultBlock").children(".input").each(function () {
                    var suffix_temp = $(this).children(".outputSuffix").val();
                    $(this).children(".output").val(defaultPath_temp_mod+"/"+suffix_temp);
                    $(this).children(".output").attr("title", defaultPath_temp_mod+"/"+suffix_temp);
                    $(this).children(".outputValue").val(suffix_temp);
                    $(this).children(".outputAddress").val(defaultPath_temp_mod+"/");
                });
            } else {
                layer.alert("请选择第一输出的路径！");
            }
        } else {
            layer.alert("请确认输出中没有文件类型输出！");
        }
        // if ($("#isDefaultPath").val()==0){
        //     $("#isDefaultPath").val(1);
        //     $('#defaultPath').children("ul").children("li").addClass("btn_thick");
        // } else {
        //     $("#isDefaultPath").val(0);
        //     $('#defaultPath').children("ul").children("li").removeClass("btn_thick");
        // }
    });
    $('#defaultOutput').click(function () {
        if(allowModification=="true") {
            $("#custom").val(0);
            $(".tabs_btn").each(function (index, elem) {
                var $elem = $(elem);
                $elem.css("display", "none");
            });
            $(".tabs_def").each(function (index, elem) {
                var $elem = $(elem);
                $elem.css("display", "block");
            });
            $('a[href="#task_0"]').tab('show');
            $("#defaultOutput").css("display", "none");
            $('#customOutput').css("display", "block");
            $('#defaultPath').css("display", "block");
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
    $('#customOutput').click(function () {
        if(allowModification=="true") {
            $("#custom").val(1);
            $(".tabs_def").each(function (index, elem) {
                var $elem = $(elem);
                $elem.css("display", "none");
            });
            $(".tabs_btn").each(function (index, elem) {
                var $elem = $(elem);
                $elem.css("display", "block");
            });
            $('a[href="#task_1"]').tab('show');
            $('#customOutput').css("display", "none");
            $('#defaultPath').css("display", "none");
            $("#defaultOutput").css("display", "block");
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
}
creatHtml = function (jsonData) {
    var output_form = document.getElementById("output_form");
    var ul = "";
    var div = "";
    var div_one = "";
    var div_one_block = "";
    ul += '<li role="presentation"><a href="#task_0" class="tabs_def" style="display: none;" aria-controls="task_0" role="tab" data-toggle="tab">默认输出</a></li>';
    div_one = "";
    for (var i = 0; i < jsonData.frontAlgorithmDetails.length; i++) {
        div_one_block = "";
        for (var j = 0; j < jsonData.frontAlgorithmDetails[i].algorithmOutputs.length; j++) {
            if (jsonData.frontAlgorithmDetails[i].algorithmOutputs[j].type!="file") {
                allIsFile = false;
            }
            div_one_block +=
                '<hr>' +
                '<div class="input">' +
                '<div class="form-group input_name"> ' +
                '<label class="col-sm-2">输出名称:<i class="input_name_text">' + jsonData.frontAlgorithmDetails[i].algorithmOutputs[j].outputName + '</i></label> ' +
                '</div> ' +
                '<input type="hidden" class="outputValue" value="">' +
                '<input type="hidden" class="outputAddress" value="">' +
                '<input type="hidden" class="outputSuffix" value="'+jsonData.frontAlgorithmDetails[i].algorithmOutputs[j].suffix+'">' +
                '<input type="text" class="form-control form-group output" readonly="readonly" id="0~' + i + '~' + j + '" onclick="getDocument(\'0~' + i + '~' + j + '\',\'' +jsonData.frontAlgorithmDetails[i].algorithmOutputs[j].suffix+'\',\''+jsonData.frontAlgorithmDetails[i].algorithmOutputs[j].type+'\')"> ' +
                '<div class="form-group"> ' +
                '<label class="col-sm-2 control-label">简介</label> ' +
                '<div class="col-sm-10"> ' +
                '<p class="form-control-static">' + jsonData.frontAlgorithmDetails[i].algorithmOutputs[j].outputIntroduction + '</p> ' +
                '</div> ' +
                '</div> ' +
                '</div>';
        }
        div_one_block = '<div class="defaultBlock" blockname="' + jsonData.frontAlgorithmDetails[i].blockName + '" blockid="' + jsonData.frontAlgorithmDetails[i].blockId + '"><div class="form-title"><h5 style="font-weight: bold;font-size: 16px;">组件名:' + jsonData.frontAlgorithmDetails[i].blockName + '</h5></div>' + div_one_block + '</div>';
        div_one += div_one_block;
    }
    div += '<div role="tabpanel" class="tab-pane fade tab-del" id="task_0"><div class="form-title"><h4>输出配置</h4></div>' + div_one + '</div>';
    for (var o = 1; o <= task_number; o++) {
        ul += '<li role="presentation"><a href="#task_' + o + '" class="tabs_btn" aria-controls="task_' + o + '" role="tab" data-toggle="tab">任务' + o + '</a></li>';
        div_one = "";
        for (var i = 0; i < jsonData.frontAlgorithmDetails.length; i++) {
            div_one_block = "";
            for (var j = 0; j < jsonData.frontAlgorithmDetails[i].algorithmOutputs.length; j++) {
                div_one_block +=
                    '<hr>' +
                    '<div class="input">' +
                    '<div class="form-group input_name"> ' +
                    '<label class="col-sm-2">输出名称:<i class="input_name_text">' + jsonData.frontAlgorithmDetails[i].algorithmOutputs[j].outputName + '</i></label> ' +
                    '</div> ' +
                    '<input type="hidden" class="outputValue" value="">' +
                    '<input type="hidden" class="outputAddress" value="">' +
                    '<input type="text" class="form-control form-group output" readonly="readonly" id="' + o + '~' + i + '~' + j + '" onclick="getDocument(\'' + o + '~' + i + '~' + j + '\',\''+jsonData.frontAlgorithmDetails[i].algorithmOutputs[j].suffix+'\',\''+jsonData.frontAlgorithmDetails[i].algorithmOutputs[j].type+'\')"> ' +
                    '<div class="form-group"> ' +
                    '<label class="col-sm-2 control-label">简介</label> ' +
                    '<div class="col-sm-10"> ' +
                    '<p class="form-control-static">' + jsonData.frontAlgorithmDetails[i].algorithmOutputs[j].outputIntroduction + '</p> ' +
                    '</div> ' +
                    '</div> ' +
                    '</div>';
            }
            div_one_block = '<div class="oneBlock" blockname="' + jsonData.frontAlgorithmDetails[i].blockName + '" blockid="' + jsonData.frontAlgorithmDetails[i].blockId + '"><div class="form-title"><h5 style="font-weight: bold;font-size: 16px;">组件名:' + jsonData.frontAlgorithmDetails[i].blockName + '</h5></div>' + div_one_block + '</div>';
            div_one += div_one_block;
        }
        div += '<div role="tabpanel" class="tab-pane fade tab-custom" id="task_' + o + '"><div class="form-title"><h4>输出配置</h4></div>' + div_one + '</div>';
    }
    ul = '<ul class="nav nav-tabs nav-pills nav-stacked table_div-ul" role="tablist" id="outputTabs">' + ul + '</ul>';
    div = '<div class="tab-content table_div-div" id="outputTabContent">' + div + '</div>';
    output_form.innerHTML += '<div class="table_div">' + ul + div + '</div>';
    $('#outputTabs a').click(function (e) {
        e.preventDefault()
        $(this).tab('show')
    });
    $('a[href="#task_1"]').tab('show');
}

getDocument = function (output_id, suffix, type) {
    $("#output_id").val(output_id);
    suffix = suffix.substring(1);
    $("#suffix").val(suffix);
    $("#type").val(type);
    layer.ready(function () {
        layer.open({
            type: 2,
            skin: 'layui-layer-lan',
            title: '输出文件选择',
            fix: false,
            scrollbar: false,
            shadeClose: true,
            maxmin: true,
            area: ['400px', '400px'],
            content:constUrl+ 'views/task/set/output/docSelect.action'
        });
    });
}